package amq.demo.cmdb.producer;
import studio.raptor.databus.client.DatabusConfig;
import studio.raptor.databus.client.DatabusProducer;
import studio.raptor.databus.client.DatabusProducerBuilder;
import studio.raptor.databus.command.*;

import javax.jms.JMSException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by koalton on 2017/7/15.<br>
 *
 * 模拟订单中心发送事件--ModifyOrder:100002
 */
public class OrderCenter {
    static SimpleDateFormat Y2S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static AtomicLong singleProcessSeq = new AtomicLong(System.currentTimeMillis() - 1494085554128L);

    public static void main(String[] args){
        try {
            DatabusConfig databusConfig = new DatabusConfig("Order");
            DatabusProducerBuilder producerBuilder = new DatabusProducerBuilder(databusConfig);
            DatabusProducer producer = producerBuilder.create();
            Event event = createEvent(10,100002,"ModifyOrder","Order");
            for (int i = 0; i < 5; i++) {
                event.getEventMetaData().setBusinessNo(nextSeq()+"");
                producer.send(event);
                System.out.println("At "+Y2S.format(new Date())+" send "+event+" success .");
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 创建事件
     * @param eventSize 消息大小
     * @param eventKindId 事件种类ID
     * @param eventName 事件名称
     * @param eventSource 事件来源，即业务中心
     * @return
     */
    private static Event createEvent(int eventSize, Integer eventKindId, String eventName, String eventSource) {
        //设定基础数据
        EventBasicData basicData = new EventBasicData();
        basicData.setSource(eventSource);
        basicData.setKindId(eventKindId);
        basicData.setName(eventName);
        basicData.setDeliverTime(Y2S.format(new Date()));
        //设置业务数据
        EventBusinessData businessData = new EventBusinessData();

        String DATA = "abcdefghijklmnopqrstuvwxyz";
        int KUNIT = 1024;
        StringBuilder sbd = new StringBuilder(eventSize * KUNIT);
        for (int i = 0; i < eventSize * KUNIT; i++) {
            sbd.append(DATA.charAt(new Random().nextInt(26)));
        }
        businessData.setContent("`~!@#%^&*()_+=-;'/,.<>|[]{}【】\\" + sbd.toString());
        businessData.setDataService("https://www.baidu.com?search=12");
        businessData.setProvideType(EventBusinessDataProvideType.PROVIDE_TYPE_CARRY);
        businessData.setDataType(EventBusinessDataType.DATATYPE_JSON);
        Event event = new Event(basicData, businessData, null);
        EventMetaData metaData = new EventMetaData();
        metaData.setBusinessNo("");
        metaData.setAreaId(1002L);
        metaData.setBusinessType(EventMetaDataBusinessType.BUSINESS_TYPE_NRM);
        event.setEventMetaData(metaData);
        return event;
    }
    private static long nextSeq() {
        long nextSeq = singleProcessSeq.getAndIncrement();
        return nextSeq;
    }

}